package com.yyfax.ph.common.share;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.widget.Toast;

import com.yyfax.ph.R;
import com.yyfax.ph.common.utils.Constants;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * 分享到不同平台的具体实现
 */
public class ShareAgent {
	protected Context mContext;

	private String mUrl;
	private String mTitle;
	private String mContent;

	private static ShareActionListener mShareActionListener;
	private MsgSender mMsgSender;

	public interface MsgSender {
		void sendMsg(String title, String content, String url);
	}

	public static class ShareAction {
		public static final int WX_FRIEND = 1;
		public static final int WX_CIRCLE = 2;
		public static final int QQ_FRIEND = 3;
		public static final int QQ_ZONE = 4;
		public static final int WEI_BO = 5;
		public static final int SEND_FRIEND = 6;
		public static final int COPY = 7;
	}

	public ShareAgent(Context ctx, String url, String title, String content, MsgSender listener) {
		mContext = ctx;
		mUrl = url;
		mTitle = title;
		mContent = content;
		mMsgSender = listener;
		mShareActionListener = new ShareActionListener(ctx, title, content, url);
	}

	public void onDestroy() {
		mShareActionListener.removeDefaultPlatformActionListener();
		mShareActionListener = null;
		mContext = null;
	}

	public void sharePlantForm(int action, int iconId) {
		ShareSDK.initSDK(mContext, Constants.SHARE_SDK_KEY);
		if(iconId == -1){
			iconId = R.drawable.ic_launcher;
		}
		switch (action) {
			case ShareAction.WX_FRIEND:
				shareWeChatFriend(iconId);
				break;
			case ShareAction.WX_CIRCLE:
				shareWXCircle(iconId);
				break;
			case ShareAction.QQ_FRIEND:
				shareQQFriend(iconId);
				break;
			case ShareAction.QQ_ZONE:
				shareQZone(iconId);
				break;
			case ShareAction.WEI_BO:
				shareWeiBo(mContext, mTitle, mContent, mUrl, iconId);
				break;
			case ShareAction.COPY:// 复制
				ClipboardManager cmb = (ClipboardManager) mContext.getSystemService(Context.CLIPBOARD_SERVICE);
				ClipData clip = ClipData.newPlainText("", mUrl);
				cmb.setPrimaryClip(clip);
				Toast.makeText(mContext, "已复制到剪切板", Toast.LENGTH_SHORT).show();
				break;
			case ShareAction.SEND_FRIEND:// 发送给好友
				if (null != mMsgSender) {
					mMsgSender.sendMsg(mTitle, mContent, mUrl);
				}
				break;
		}
	}

	public static void shareWeiBo(Context ctx, String title, String content, String url, int iconId) {
		if (!TextUtils.isEmpty(url)) {
			Platform weibo = ShareSDK.getPlatform(ctx, SinaWeibo.NAME);
			weibo.setPlatformActionListener(mShareActionListener);
			if (!weibo.isAuthValid()) {
				HashMap<String, Object> map = new HashMap<>();
				map.put("AppKey", Constants.WEI_BO_APP_KEY);
				map.put("AppSecret", Constants.WEI_BO_APP_SECRET);
				map.put("RedirectUrl", Constants.WEI_BO_REDIRECT_URL);
				map.put("ShareByAppClient", Constants.WEI_BO_SHARE_BY_APP_CLIENT);
				map.put("Enable", Constants.WEI_BO_ENABLE);
				ShareSDK.setPlatformDevInfo(SinaWeibo.NAME, map);
				weibo.authorize();
			} else {
				cn.sharesdk.sina.weibo.SinaWeibo.ShareParams sp = new cn.sharesdk.sina.weibo.SinaWeibo.ShareParams();
				if (TextUtils.isEmpty(content)) {
					content = title;
				}
				sp.setText(content + "\r\n链接地址：" + url);
				sp.setShareType(Platform.SHARE_TEXT);
				Bitmap bitmap = BitmapFactory.decodeResource(ctx.getResources(), iconId);
				sp.setImageData(bitmap);
				weibo.share(sp);
			}
		} else {
			Toast.makeText(ctx, "分享失败，无效链接地址", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 分享到手机QQ
	 */
	private void shareQQFriend(int iconId) {
		if (!TextUtils.isEmpty(mUrl)) {
			HashMap<String, Object> map = new HashMap<>();
			map.put("Id", Constants.QQ_FRIEND_ID);
			map.put("SortId", Constants.QQ_FRIEND_SORT_ID);
			map.put("AppId", Constants.QQ_APP_ID);
			map.put("AppKey", Constants.QQ_APP_KEY);
			map.put("ShareByAppClient", Constants.QQ_SHARE_BY_APP_CLIENT);
			map.put("Enable", Constants.QQ_ENABLE);

			Platform circle = ShareSDK.getPlatform(mContext, QQ.NAME);
			circle.SSOSetting(false);
			circle.setPlatformActionListener(mShareActionListener);
			ShareSDK.setPlatformDevInfo(QQ.NAME, map);

			// ShareParams sp = new ShareParams();
			// sp.setTitle("测试分享的标题测试分享的标题测试分享的标题测试分享的标题测试分享的");
			// sp.setTitleUrl("http://www.ifeng.com/"); // 标题的超链接
			// sp.setText("测试分享的文本 ");
			// sp.setImageUrl("http://f1.sharesdk.cn/imgs/2014/02/26/owWpLZo_638x960.jpg");

			QQ.ShareParams sp = new QQ.ShareParams();
			sp.setTitle(mTitle);
			sp.setTitleUrl(mUrl);
			if (TextUtils.isEmpty(mContent)) {
				mContent = mTitle;
			}
			sp.setText(mContent);

			Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), iconId);
			sp.setImageData(bitmap);
			circle.share(sp);
		} else {
			Toast.makeText(mContext, "分享失败，无效链接地址", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 分享到微信好友
	 */
	private void shareWeChatFriend(int iconId) {
		if (!TextUtils.isEmpty(mUrl)) {
			Platform circle = ShareSDK.getPlatform(mContext, Wechat.NAME);
			if (!circle.isClientValid()) {
				Toast.makeText(mContext, "分享失败，请先安装微信", Toast.LENGTH_SHORT).show();
				return;
			}
			circle.setPlatformActionListener(mShareActionListener);

			HashMap<String, Object> map = new HashMap<>();
			map.put("AppId", Constants.WE_CHAT_APP_ID);
			map.put("AppSecret", Constants.WE_CHAT_SECRET);
			map.put("Enable", Constants.WE_CHAT_ENABLE);
			map.put("BypassApproval", Constants.WE_CHAT_BYPASS_APPROVAL);

			ShareSDK.setPlatformDevInfo(Wechat.NAME, map);

			cn.sharesdk.wechat.friends.Wechat.ShareParams sp = new cn.sharesdk.wechat.friends.Wechat.ShareParams();
			sp.setShareType(Platform.SHARE_WEBPAGE);
			sp.setTitle(mTitle);

			sp.setText(mContent);

			Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), iconId);
			sp.setImageData(bitmap);
			sp.setUrl(mUrl);
			circle.share(sp);
		} else {
			Toast.makeText(mContext, "分享失败，无效链接地址", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 分享到朋友圈
	 */
	private void shareWXCircle(int iconId) {
		if (!TextUtils.isEmpty(mUrl)) {
			Platform circle = ShareSDK.getPlatform(mContext, WechatMoments.NAME);
			if (!circle.isClientValid()) {
				Toast.makeText(mContext, "分享失败，请先安装微信", Toast.LENGTH_SHORT).show();
				return;
			}
			circle.setPlatformActionListener(mShareActionListener);

			HashMap<String, Object> map = new HashMap<>();
			map.put("AppId", Constants.WE_CHAT_APP_ID);
			map.put("AppSecret", Constants.WE_CHAT_SECRET);
			map.put("Enable", Constants.WE_CHAT_ENABLE);
			map.put("BypassApproval", Constants.WE_CHAT_BYPASS_APPROVAL);
			ShareSDK.setPlatformDevInfo(WechatMoments.NAME, map);

			cn.sharesdk.wechat.moments.WechatMoments.ShareParams sp = new cn.sharesdk.wechat.moments.WechatMoments.ShareParams();
			sp.setShareType(Platform.SHARE_WEBPAGE);
			sp.setTitle(mTitle);
			sp.setText(mContent);

			Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), iconId);
			sp.setImageData(bitmap);
			sp.setUrl(mUrl);
			circle.share(sp);
		} else {
			Toast.makeText(mContext, "分享失败，无效链接地址", Toast.LENGTH_SHORT).show();
		}
	}

	/**
	 * 分享到QZone
	 */
	private void shareQZone(int iconId) {
		if (!TextUtils.isEmpty(mUrl)) {
			HashMap<String, Object> map = new HashMap<>();
			map.put("Id", Constants.QQ_ID);
			map.put("Sort", Constants.QQ_SORT_ID);
			map.put("AppId", Constants.QQ_APP_ID);
			map.put("AppKey", Constants.QQ_APP_KEY);
			map.put("ShareByAppClient", Constants.QQ_SHARE_BY_APP_CLIENT);
			map.put("Enable", Constants.QQ_ENABLE);
			ShareSDK.setPlatformDevInfo(QZone.NAME, map);

			Platform qZone = ShareSDK.getPlatform(mContext, QZone.NAME);
			qZone.setPlatformActionListener(mShareActionListener);

			QZone.ShareParams sp = new QZone.ShareParams();
			sp.setShareType(Platform.SHARE_IMAGE);
			sp.setTitle(mTitle);
			sp.setTitleUrl(mUrl);
			sp.setText(mContent);

			Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), iconId);
			sp.setImageData(bitmap);
			sp.setSite(mContext.getResources().getString(R.string.app_name));

			qZone.share(sp);
		} else {
			Toast.makeText(mContext, "分享失败，无效链接地址", Toast.LENGTH_SHORT).show();
		}
	}

	public static ShareActionListener getmShareActionListener() {
		return mShareActionListener;
	}

	public void setTitle(String title) {
		mTitle = title;
	}

	public void setContent(String content) {
		mContent = content;
	}
}
